<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_reporting extends CI_Controller {
    private $limit = 20;
    private $title = "Admin reporting";
    function __construct() {
        parent::__construct();
        $this->load->model('model_reporting', 'm_report', TRUE);
    }
    
    function index() {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            
            $data = array(
                'web_title' =>  $this->title.' List',
                'main_view' =>  'mimin/reporting/index_new',
                'list_data' =>  $this->m_report->get_all(),
            );
            $this->load->view('mimin/template/template_table_search',$data);
        }
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $this->form_validation->set_rules('judul','Judul','required');
            $this->form_validation->set_rules('content','Isi Konten','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'             =>  'Data Berita Baru',
                    'msg_error_username'    =>  form_error('judul','<caption>','</caption>'),
                    'msg_error_name'        =>  form_error('content','<caption>','</caption>'),
                    'main_view'             =>  "mimin/news/add"
                );
                $this->parser->parse("mimin/template/template_editor",$data);
            }
            else{
                $config['upload_path'] = './content/media/news/';
                $config['allowed_types'] = 'jpg|jpeg|png';
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()){
                    $data = array(
                        "main_view"     =>  "mimin/news/add",
                        "web_title"     =>  "Upload New News Data",
                        "error"         =>  $this->upload->display_errors()
                    );
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:m:s");
                    $upload_image = $this->upload->data();
                    $isi = array(
                        'judul'         =>  $this->input->post('judul'),
                        'content'       =>  $this->input->post('content'),
                        'datecreated'   =>  $curdate,
                        'datemodified'  =>  $curdate,
                        'state_id'      =>  $this->session->userdata('admin_user')->state_id,
                        'image'         =>  'content/media/news/'.$upload_image['file_name']
                    );
                    if($this->m_news->save($isi)){
                        $data = array(
                            "main_view"     =>  "mimin/news/add",
                            "web_title"     =>  "Upload New News Data",
                            "success_status"    =>  1
                        );
                        $this->load->view("mimin/template/template_editor",$data);
                    }
                }
            }
        }
    }
    function detail($id=""){
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d h:i:s");
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_reporting","refresh");
        }
        else{
            $count = $this->m_report->select_by_id($id,"count");
            if($count > 0){
                //update menjadi read=1
                $array = array(
                    'read'  =>  1
                );
                $status = $this->m_report->update($id,$array);
                $data = array(
                    "main_view"     =>  "mimin/reporting/detail",
                    "web_title"     =>  $this->title." Detail",
                    "list_data"     =>  $this->m_report->select_by_id($id)
                );
                $this->load->view("mimin/template/template_table",$data);
            }
            else{
                redirect("admin_reporting","refresh");
            }
        }
    }
    function mark_as_read(){
        $this->form_validation->set_rules('id_kasus','id','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "Missing Data ID"
            );
            echo json_encode($output);exit();
        }
        else{
            $count = $this->m_report->count_by_id($this->input->post("id_kasus"));
            if($count == 0){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Missing Data, please refresh the page"
                );
                echo json_encode($output);exit();
            }
            else{
                $array = array(
                    'status'  =>  1
                );
                $status = $this->m_report->update($this->input->post("id_kasus"),$array);
                if(!$status){
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Error Update Data"
                    );
                    echo json_encode($output);exit();
                }
                else{
                    $output = array(
                        'status'    =>  1,
                        'msg'       =>  "Successfully"
                    );
                    echo json_encode($output);exit();
                }
            }
        }
    }
    function delete(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            $this->form_validation->set_rules('id_kasus','id','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Missing Data ID"
                );
                echo json_encode($output);exit();
            }
            else{
                $count = $this->m_report->count_by_id($this->input->post("id_kasus"));
                if($count == 0){
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Missing Data, please refresh the page"
                    );
                    echo json_encode($output);exit();
                }
                else{
                    $status = $this->m_report->delete($this->input->post("id_kasus"));
                    if(!$status){
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Error Delete Data"
                        );
                        echo json_encode($output);exit();
                    }
                    else{
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Successfully Delete Data, page will be refresh in 3 seconds"
                        );
                        echo json_encode($output);exit();
                    }
                }
            }
        }
        else{
            die("Cannot access directly");
        }
        
    }
}

/* End of file admin_reporting.php */
/* Location: ./application/controllers/admin_event.php */